package com.carsite.webapp.help;

import java.util.Iterator;
import java.util.List;

import com.carsite.model.Branch;
import com.carsite.model.Department;

public class BranchDeptHelp {
	
    //机构部门tree
	public static String dumpBranchDeptScript(List menus,String isParent,String isDiv) {
		String scriptStr = "";
		if("true".equals(isDiv)){
			 scriptStr = "var rootTree = new WebFXTree('机构部门');\r\n";
			 
			 if("true".equals(isParent)){
		     	scriptStr += "var root_item = new WebFXTreeItem('置空','javascript:onSel(0,0)');\r\n";
		     }else{
		     	scriptStr += "var root_item = new WebFXTreeItem('置空','javascript:onSel(0,0)');\r\n";
		     }
			 scriptStr += "rootTree.add(root_item);\r\n";
			 scriptStr = scriptStr+"rootTree.setBehavior('explorer');\r\n";  	     	 
		}else{
		     scriptStr = "var rootTree = new WebFXTree('机构部门');\r\n";
		     scriptStr = scriptStr+"rootTree.setBehavior('explorer');\r\n";
		}
		
		Iterator it = menus.iterator();
	    return scriptStr+makeTree("rootTree",it);
	}
	
	 
	
	  private static String makeTree(String parentName,Iterator it){
	  	String scriptStr ="";
	  
	  	while(it.hasNext()){
	           Branch branch = (Branch)it.next();
	           
	           String name =  branch.getBranchName();
	           Long id = branch.getBranchId();
	          
	           scriptStr = scriptStr+"var item_"+name+" = new WebFXTreeItem('"+name+"','javascript:onSel("+id+",1)');\r\n";
        	   scriptStr = scriptStr+ "item_"+name+".icon =  'tree/foldericon.png';\r\n";
        	   scriptStr = scriptStr+ "item_"+name+".openIcon =  'tree/openfoldericon.png';\r\n";
        	
        	   
        	   if(branch.getDepts().size()>0){
	                
        		   Iterator itDept = branch.getDepts().iterator();
	                
	               while(itDept.hasNext()){
	            	  Department dept = (Department)itDept.next();
	            	  String deptName =  dept.getDeptName();
	   	              Long   deptId = dept.getDeptId();
	   	          
	            	  
	            	   scriptStr = scriptStr+"var item_d_"+deptName+" = new WebFXTreeItem('"+deptName+"','javascript:onSel("+deptId+",0)');\r\n"; 
		        	   scriptStr = scriptStr+ "item_d_"+deptName+".icon = 'tree/file.png';\r\n";
		        	   
		        	   scriptStr = scriptStr+ "item_"+name+".add(item_d_"+deptName+");\r\n";
	               } 
	                  
	            }
        	    
	           
	           if(branch.getChildren().size()>0){
	               Iterator itOne = branch.getChildren().iterator();
	                scriptStr = scriptStr+ makeTree("item_"+name,itOne);
	            }
	           scriptStr = scriptStr+ parentName+".add(item_"+name+");\r\n";
        } 
	  	return scriptStr;
     }
	  
		//机构tree
		public static String dumpBranchScript(List menus,String isParent,String isDiv) {
			String scriptStr = "";
			if("true".equals(isDiv)){
				 scriptStr = "var rootTree = new WebFXTree('选择机构');\r\n";
				 
				 if("true".equals(isParent)){
			     	scriptStr += "var root_item = new WebFXTreeItem('置空','javascript:onSel(0,0)');\r\n";
			     }else{
			     	scriptStr += "var root_item = new WebFXTreeItem('置空','javascript:onSel(0,0)');\r\n";
			     }
				 scriptStr += "rootTree.add(root_item);\r\n";
				 scriptStr = scriptStr+"rootTree.setBehavior('explorer');\r\n";  	     	 
			}else{
			     scriptStr = "var rootTree = new WebFXTree('机构部门');\r\n";
			     scriptStr = scriptStr+"rootTree.setBehavior('explorer');\r\n";
			}
			
			Iterator it = menus.iterator();
		    return scriptStr+makeBranchTree("rootTree",it);
		}
		
		 
		
		  private static String makeBranchTree(String parentName,Iterator it){
		  	String scriptStr ="";
		  
		  	while(it.hasNext()){
		           Branch branch = (Branch)it.next();
		           
		           String name =  branch.getBranchName();
		           Long id = branch.getBranchId();
		          
		           scriptStr = scriptStr+"var item_"+name+" = new WebFXTreeItem('"+name+"','javascript:onSel("+id+",1)');\r\n";
	        	   
		           if(branch.getChildren().size()>0){
		               Iterator itOne = branch.getChildren().iterator();
		                scriptStr = scriptStr+ makeTree("item_"+name,itOne);
		            }
		           scriptStr = scriptStr+ parentName+".add(item_"+name+");\r\n";
	        } 
		  	return scriptStr;
	     }
	  
	  
	  
	  
	 /* public static String dumpSelectedAreaScript(List areas) {
			String scriptStr = "";
			
			scriptStr = "var areaTree = new WebFXTree('选择区域');\r\n";
			scriptStr = scriptStr+"areaTree.setBehavior('explorer');\r\n";
			
			Iterator it = areas.iterator();
		    return scriptStr+makeSelectedAreaTree("areaTree",it);
		}
	  
	  private static String makeSelectedAreaTree(String parentName,Iterator it){
		  	String scriptStr ="";
		  
		  	while(it.hasNext()){
		            Area area = (Area)it.next();
		           String name =  area.getName();
		           Long areaId = area.getAreaId();
		           String tmpName = name +"-"+ areaId;
		           scriptStr = scriptStr+"var item_"+name+" = new WebFXCheckBoxTreeItem('"+tmpName+"','javascript:onSelArea("+areaId+")');\r\n";
		        
		           if(area.getChildren().size()>0){
		            Iterator itOne = area.getChildren().iterator();
		            scriptStr = scriptStr+ makeSelectedAreaTree("item_"+name,itOne);
		        }
		           scriptStr = scriptStr+ parentName+".add(item_"+name+");\r\n";
	        } 
		  	return scriptStr;
	     }*/
	  
}
